package PhotoController;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.PagingManager;


public class PhotoYoungAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String sessionID  = null;
	private HttpSession session = null;
     PhotoYoungDAO photoYoungDAO=new PhotoYoungDAO();
   
    public PhotoYoungAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}
	private int currentPage=1;
	
 	private PagingManager paging;
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		PhotoYoungBean photoYoungBean= null;
		RequestDispatcher view = null;
		PrintWriter out = response.getWriter();
		if(action.equals("pt_young_list_main")){
			//ArrayList list =photoYoungDAO.pt_young_list_main();
			//request.setAttribute("list", list);
			
			
			//request.getRequestDispatcher("/Main/OldMain/home_photo3.jsp").forward(request, response);
		}
		else if(action.equals("pt_young_list")){
			String mem_nick=request.getParameter("nick");
			int level=photoYoungDAO.young_level(mem_nick);
			if(request.getParameter("currentPage")!=null){
				currentPage=Integer.parseInt(request.getParameter("currentPage"));
			}
			HashMap param=new HashMap();
			param = new HashMap<String, Object>();
			param.clear();
			param.put("pageSize", 3 );
			param.put("currentPage", currentPage );
			param.put("pageGroupSize", 5 );
			int pageNo = ((Integer)param.get("currentPage") -1) * (Integer)param.get("pageSize");
			int endNum = pageNo + (Integer)param.get("pageSize");
			param.put( "startNum", pageNo );
			//param.put( "recordCount", (Integer)param.get("pageSize") );
			ArrayList list =photoYoungDAO.pt_young_list(param);
			int totalCount = photoYoungDAO.pyTotalCount(param);
			param.put("totalCount", totalCount);
			paging=new common.PagingManager();
			paging.setPageNo(param);
			session = request.getSession();
			session.setAttribute("list", list);
			session.setAttribute("paging", paging.getReturnHtml());
			request.setAttribute("page","/photo/PtYoung/PT_Young_List");
			String photo=null;
			if(level==2){
			session.setAttribute("photo", "young_photo");
			view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
			
			}else if(level==1){
			session.setAttribute("photo", "young_photo2");
			view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
			}
			view.forward(request, response);
		}else if(action.equals("pt_young_recom")){
			String mem_nick=request.getParameter("nick");
				System.out.println("왜안되니?"+request.getParameter("id"));
				int id=Integer.parseInt(request.getParameter("id"));
				ArrayList list=photoYoungDAO.pt_young_recom(id);
				
				int level=photoYoungDAO.young_level(mem_nick);
				String photo=null;
				if(level==2){
					session.setAttribute("photo", "young_photo");
				
				}else if(level==1){
					session.setAttribute("photo", "young_photo2");
				}
				request.setAttribute("list", list);
				request.setAttribute("id", id);
				request.getRequestDispatcher("/photo/PtYoung/PT_Young_Comment.jsp").forward(request, response);
		}else if(action.equals("pt_young_recom_write")){
			
			int id=Integer.parseInt(request.getParameter("id"));
			String bc_contents=request.getParameter("bc_contents");
			String bc_nick=request.getParameter("bc_nick");
			
		    photoYoungDAO.pt_young_recom_write(id,bc_contents,bc_nick);
			
			request.getRequestDispatcher("/PhotoYoungAction.do?action=pt_young_recom&id="+id).forward(request, response);
		}else if(action.equals("pt_young_com_cancle")){
			int com_id=Integer.parseInt(request.getParameter("id"));
			int babyplus_id=Integer.parseInt(request.getParameter("babyplus_id"));
			photoYoungDAO.pt_young_com_cancle(com_id,babyplus_id);
			request.getRequestDispatcher("/PhotoYoungAction.do?action=pt_young_recom&id="+babyplus_id).forward(request, response);
		}
		
	}

}
